package com.raja.servingStation.dao;

import com.raja.servingStation.datasources.RepairSql2oTemp;
import com.raja.servingStation.datasources.Sql2oConnection;
import com.raja.servingStation.entity.RepairPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.sql2o.Connection;
import org.sql2o.Query;

import java.math.BigDecimal;
import java.util.List;

@Repository
public class OrderClaimDao {

    @Autowired
    private RepairSql2oTemp repairSql2oTemp;

    public int getOrderClaimCountById(Integer orderId,Integer orderType) throws Exception {
            String sql = " SELECT  " +
                    "  count(*)  " +
                    "FROM  " +
                    "  t_order_claim toc  " +
                    "WHERE  1=1 " ;
                    if(orderType == 1) {
                       sql = sql +   " and toc.CLAIM_STATE = 0 ";
                    }
                    sql = sql +  "and toc.ORDER_TYPE = :orderType  " +
                    "AND toc.ORDER_ID =  :orderId" ;
            try (Connection con = repairSql2oTemp.getSql2o().open()) {
                Query query = Sql2oConnection.createQuery(con,sql);
                query.addParameter("orderId",orderId);
                query.addParameter("orderType",orderType);
                return  ((Long)query.executeScalar()).intValue();
            }
    }

    public void commitOrderClaim(Integer orderId,Integer orderType) throws Exception {
        String sql = " INSERT INTO   t_order_claim   (  " +
                "    ORDER_ID  ,  " +
                "    CLAIM_STATE  ,  " +
                "    CLAIM_TIME  ,    " +
                "    ORDER_TYPE    " +
                ")  " +
                "VALUES (:orderId,0,now(),:orderType )" ;
        try (Connection con = repairSql2oTemp.getSql2o().open()) {
            Query query = Sql2oConnection.createQuery(con,sql);
            query.addParameter("orderId",orderId);
            query.addParameter("orderType",orderType);
            query.executeUpdate();
        }
    }
}
